package org.or5e.service.core.ds.event;

import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.or5e.service.core.dao.AlertMaster;
import org.or5e.service.core.dao.CommentMaster;
import org.or5e.service.core.dao.EventMaster;
import org.or5e.service.core.dao.FriendMaster;
import org.or5e.service.core.dao.UserLogin;
import org.or5e.service.core.dao.WishMaster;
import org.or5e.service.core.ds.BasicDataSource;
import org.or5e.service.core.ds.DataSourceFactory;
import org.or5e.service.core.ds.login.LoginDataSource;
import org.or5e.service.core.sm.DAOSession;
import org.or5e.service.core.sm.SessionManager;

public class EventDataSourceImpl extends BasicDataSource implements EventDataSource{
	public EventDataSourceImpl(SessionFactory sessionFactory, SessionManager sessionManager) {
		super(sessionFactory, sessionManager);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EventMaster> getEvents(String sessionID) {
		List<EventMaster> eMaster = null;
		Session session = this.sessionFactory.openSession();
		DAOSession userSession = sessionManager.getSession(sessionID);
		
		if(userSession != null) {
			Integer userID = (Integer)userSession.get("USERID");
			if(userID != null) {
				eMaster = (List<EventMaster>)session.createQuery("from EventMaster as e where e.userID='"+userID+"'").list();
			}
		}
		
		clearSession(session);
		return eMaster;
	}
	private void clearSession(Session session) {
		session.flush();
		session.close();
	}

	@Override
	public void removeEvent(EventMaster eMaster) {
		
	}

	@Override
	public void updateEvent(EventMaster eMaster) {
		
	}

	@Override
	public void addEvents(List<EventMaster> eMaster) {
		
	}

	@Override
	public void addEvent(EventMaster eMaster) {
		
	}
	public static void main(String[] args) {
		LoginDataSource lds = (LoginDataSource)DataSourceFactory.getDataSourceFactory().getDataSource(LoginDataSource.class);
		UserLogin uLogin = new UserLogin();
		uLogin.setLoginID("mylife.ponraj@gmail.com");
		uLogin.setPassword("demo");
		
		String sessionID = lds.authenticateUser(uLogin);
		System.out.println("Session ID: "+sessionID);
		
		EventDataSource eds = (EventDataSource) DataSourceFactory.getDataSourceFactory().getDataSource(EventDataSource.class);
		List<EventMaster> events = eds.getEvents(sessionID);
		System.out.println("Event size :"+events.size());
		for (EventMaster eventMaster : events) {
			Set<AlertMaster> alertMode = eventMaster.getAlertMode();
			for (AlertMaster alertMaster : alertMode) {
				System.out.println(alertMaster.getAlertName());
			}
		}		
		eds.closeDataSource();
		lds.closeDataSource();
	}
	@Override
	public String getName() {
		return "EventDataSourceImpl";
	}
	@Override
	public List<CommentMaster> getComments(String eventID) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<WishMaster> getWishList(String eventID) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<FriendMaster> getFriendList(String userID) {
		// TODO Auto-generated method stub
		return null;
	}
}